package com.yunze.web.controller.yunze.bill;

import com.alibaba.fastjson.JSON;
import com.yunze.common.annotation.Log;
import com.yunze.common.core.domain.entity.SysUser;
import com.yunze.common.core.domain.model.LoginUser;
import com.yunze.common.enums.BusinessType;
import com.yunze.common.utils.Arith;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.ip.IpUtils;
import com.yunze.common.utils.spring.SpringUtils;
import com.yunze.common.utils.yunze.AesEncryptUtil;
import com.yunze.framework.web.service.TokenService;
import com.yunze.system.service.yunze.IYzCardPacketService;
import com.yunze.system.service.yunze.bill.IYzBillService;
import com.yunze.web.core.config.MyBaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 *  客户账单
 *  2023年7月17日14:25:13
 * @author root
 */
@Api("平台账单管理")
@RestController
@RequestMapping("/yunze/bill")
public class YzBillController extends MyBaseController {

    @Autowired
    private IYzBillService iYzBillService;


    /**
     * 账单列表
     */
    @Log(title = "平台账单列表", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('yunze:bill:list')")
    @PostMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public String add(@RequestBody String Pstr) {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
            SysUser currentUser = loginUser.getUser();
            String dept_id = currentUser.getDeptId().toString();
            Parammap.put("dept_id",dept_id);

            //return MyRetunSuccess(iYzBillService.getList(Parammap), null);
            return MyRetunSuccess(iYzBillService.getListCopyZ(Parammap), null);

        } catch (Exception e) {
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:tariffPlan:add  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ", e.toString());
        }
        return Myerr("平台账单查询 异常！");
    }

    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('yunze:bill:export')")
    public String exportBill(@RequestBody String Pstr) throws Exception {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try{
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr)); //参数解析
            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
            SysUser currentUser = loginUser.getUser();
            String dept_id = currentUser.getDeptId().toString();
            Parammap.put("dept_id",dept_id); //放入dept_id
            return MyRetunSuccess(iYzBillService.exportBillCopy(Parammap,currentUser), null);
        }catch (Exception e){
            return Myerr("账单导出错误!");
        }

    }

    @PostMapping("/plist")
    public String getPstr(@RequestBody String Pstr) throws Exception {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if (Pstr != null) {
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try{
            Pstr = AesEncryptUtil.desEncrypt(Pstr);
           // Parammap.putAll(JSON.parseObject((String) Pstr)); //参数解析
            Parammap.put("t_id",Pstr);
            return MyRetunSuccess(iYzBillService.plist(Parammap), null);
        }catch (Exception e){
            return Myerr("账单导出错误!");
        }

    }


}
